package com.kehutong.payfees.util;

import com.kehutong.common.DoveClient;
import com.kehutong.common.util.Token;
import org.coraframework.converter.util.StringUtil;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.util.Objects;

import java.util.*;

public class BasicClient {

    @Inject
    private DoveClient doveClient;

    public JSONObject getHouseById(Token token, String id) throws Exception {
        JSONObject result = doveClient.post("/basic/house/service/get", http->{
            http.addHeader("companyNo", token.getCompanyNo());
            http.addParam("id", id);
        });

        JSONObject data = result.getJSONObject("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }

    public JSONObject getHouseById(String companyNo, String id) throws Exception {
        JSONObject result = doveClient.post("/basic/house/service/get", http->{
            http.addHeader("companyNo", companyNo);
            http.addParam("id", id);
        });

        JSONObject data = result.getJSONObject("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }

    public JSONArray getHouseByIds(String companyNo, List<String> ids) throws Exception {
        JSONObject result = doveClient.post("/basic/house/service/list", http->{
            JSONObject body = new JSONObject();
            body.put("id", ids);

            http.addHeader("companyNo", companyNo);
            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }

    public JSONArray getHouseByIds(Token token, List<String> ids) throws Exception {
        JSONObject result = doveClient.post("/basic/house/service/list", http->{
            JSONObject body = new JSONObject();
            body.put("id", ids);

            http.addHeader("companyNo", token.getCompanyNo());
            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }

    public Map<String, JSONObject> getHouseByIdsMap(Token token, List<String> ids) throws Exception {
        JSONArray data = getHouseByIds(token, ids);
        if (Objects.isNull(data)) {
            return new HashMap<>();
        }

        Map<String, JSONObject> houseMap = new HashMap<>();
        if(Objects.nonNull(data)){
            data.forEach(item->{
                JSONObject d = (JSONObject) item;
                houseMap.put(d.getString("id"), d);
            });
        }

        return houseMap;
    }

    public JSONArray getHouses(Token token, JSONObject json) throws Exception {
        JSONObject result = doveClient.post("/basic/house/service/list", http->{
            JSONObject body = new JSONObject();
            JSONArray communityNo = json.getJSONArray("communityNo");
            if(Objects.nonEmpty(communityNo)){
                body.put("communityNo", communityNo);
            }

            JSONArray communityNos = json.getJSONArray("departmentIds");
            if(Objects.nonEmpty(communityNos)){
                body.put("communityNo", communityNos);
            }

            String buildNo = json.getString("buildingNo");
            if(Objects.nonEmpty(buildNo)){
                body.put("buildNo", buildNo);
            }

            buildNo = json.getString("buildNo");
            if(Objects.nonEmpty(buildNo)){
                body.put("buildNo", buildNo);
            }

            String buildName = json.getString("buildingName");
            if(Objects.nonEmpty(buildName)){
                body.put("buildName", buildName);
            }

            buildName = json.getString("buildName");
            if(Objects.nonEmpty(buildName)){
                body.put("buildName", buildName);
            }

            String ownerMobile = json.getString("ownerMobile");
            if(Objects.nonEmpty(ownerMobile)){
                body.put("ownerMobile", ownerMobile);
            }

            String ownerName = json.getString("ownerName");
            if(Objects.nonEmpty(ownerName)){
                body.put("ownerName", ownerName);
            }

            String ownerNo = json.getString("ownerNo");
            if(Objects.nonEmpty(ownerNo)){
                body.put("ownerNo", ownerNo);
            }

            String floorName = json.getString("floorName");
            if(Objects.nonEmpty(floorName)){
                body.put("floorName", floorName);
            }

            String unitName = json.getString("unitName");
            if(Objects.nonEmpty(unitName)){
                body.put("unitName", unitName);
            }

            String name = json.getString("name");
            if(Objects.nonEmpty(name)){
                body.put("name", name);
            }
            http.addHeader("companyNo", token.getCompanyNo());
            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");

        if (Objects.isNull(data)) {
            return new JSONArray();
        }

        return data;
    }

    public JSONArray getHouses(String companyNo, JSONObject json) throws Exception {
        JSONObject result = doveClient.post("/basic/house/service/list", http->{
            JSONObject body = new JSONObject();
            JSONArray communityNo = json.getJSONArray("communityNo");
            if(Objects.nonEmpty(communityNo)){
                body.put("communityNo", communityNo);
            }

            JSONArray communityNos = json.getJSONArray("departmentIds");
            if(Objects.nonEmpty(communityNos)){
                body.put("communityNo", communityNos);
            }

            String buildNo = json.getString("buildingNo");
            if(Objects.nonEmpty(buildNo)){
                body.put("buildNo", buildNo);
            }

            buildNo = json.getString("buildNo");
            if(Objects.nonEmpty(buildNo)){
                body.put("buildNo", buildNo);
            }

            String buildName = json.getString("buildingName");
            if(Objects.nonEmpty(buildName)){
                body.put("buildName", buildName);
            }

            buildName = json.getString("buildName");
            if(Objects.nonEmpty(buildName)){
                body.put("buildName", buildName);
            }

            String ownerMobile = json.getString("ownerMobile");
            if(Objects.nonEmpty(ownerMobile)){
                body.put("ownerMobile", ownerMobile);
            }

            String ownerName = json.getString("ownerName");
            if(Objects.nonEmpty(ownerName)){
                body.put("ownerName", ownerName);
            }

            String ownerNo = json.getString("ownerNo");
            if(Objects.nonEmpty(ownerNo)){
                body.put("ownerNo", ownerNo);
            }

            String floorName = json.getString("floorName");
            if(Objects.nonEmpty(floorName)){
                body.put("floorName", floorName);
            }

            String unitName = json.getString("unitName");
            if(Objects.nonEmpty(unitName)){
                body.put("unitName", unitName);
            }

            String name = json.getString("name");
            if(Objects.nonEmpty(name)){
                body.put("name", name);
            }
            http.addHeader("companyNo", companyNo);
            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");

        if (Objects.isNull(data)) {
            return new JSONArray();
        }

        return data;
    }

    public JSONObject getCommunityById(Token token, String id) throws Exception {
        JSONObject result = doveClient.post("/basic/community/service/get", http->{
            http.addHeader("companyNo", token.getCompanyNo());
            http.addParam("id", id);
        });

        JSONObject data = result.getJSONObject("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }

    public JSONObject getCommunityById(String companyNo, String id) throws Exception {
        JSONObject result = doveClient.post("/basic/community/service/get", http->{
            http.addHeader("companyNo", companyNo);
            http.addParam("id", id);
        });

        JSONObject data = result.getJSONObject("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }

    public JSONArray getCommunityByIds(Token token, List<String> ids) throws Exception {
        JSONObject result = doveClient.post("/basic/community/service/list", http->{
            http.addHeader("companyNo", token.getCompanyNo());

            JSONObject body = new JSONObject();
            body.put("id", ids);

            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }

    public JSONObject getEmployeeById(Token token, String id) throws Exception {
        JSONObject result = doveClient.post("/basic/employee/service/list", http->{
            http.addHeader("companyNo", token.getCompanyNo());

            JSONObject body = new JSONObject();
            body.put("id", id);

            http.setBody(body.toString());
        });

        JSONObject data = result.getJSONObject("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;

    }

    public JSONArray getEmployees(String companyNo, JSONObject json) throws Exception {

        JSONObject result = doveClient.post("/basic/employee/service/list", http->{
            http.addHeader("companyNo", companyNo);

            JSONObject body = new JSONObject();
            body.put("departments", json.getString("departments"));

            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }

    public JSONArray getEmployees(Token token, JSONObject json) throws Exception {

        JSONObject result = doveClient.post("/basic/employee/service/list", http->{
            http.addHeader("companyNo", token.getCompanyNo());

            JSONObject body = new JSONObject();
            body.put("departments", json.getString("departments"));

            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }

    public JSONArray getEmployeeByIds(Token token, List<String> ids) throws Exception {

        JSONObject result = doveClient.post("/basic/employee/service/list", http->{
            http.addHeader("companyNo", token.getCompanyNo());

            JSONObject body = new JSONObject();
            body.put("id", ids);

            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }

    public JSONArray getEmployeeByIds(String companyNo, List<String> ids) throws Exception {

        JSONObject result = doveClient.post("/basic/employee/service/list", http->{
            http.addHeader("companyNo", companyNo);

            JSONObject body = new JSONObject();
            body.put("id", ids);

            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }

    public JSONArray getCommunitys(Token token, JSONObject json) throws Exception {
        JSONObject result = doveClient.post("/basic/community/service/list", http->{
            http.addHeader("companyNo", token.getCompanyNo());

            JSONObject body = new JSONObject();
            JSONArray ids = json.getJSONArray("departmentIds");
            if(Objects.nonNull(ids)){
                body.put("parent_id", ids);
            }

            String area_names = json.getString("area_names");
            if(Objects.nonEmpty(area_names)){
                body.put("area_names", area_names);
            }

            String name = json.getString("name");
            if(Objects.nonNull(name)){
                body.put("name", name);
            }

            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");

        if (Objects.isNull(data)) {
            return new JSONArray();
        }

        return data;
    }

    public Map<String,JSONObject> getCommunityMap(Token token) throws Exception {
        JSONObject result = doveClient.post("/basic/community/service/list", http->{
            http.addHeader("companyNo", token.getCompanyNo());
        });

        JSONArray data = result.getJSONArray("data");

        if (Objects.isNull(data)) {
            return null;
        }

        Map<String,JSONObject> communityMap = new HashMap<>();
        data.forEach(item->{
            JSONObject obj = (JSONObject) item;
            communityMap.put(obj.getString("id"), obj);
        });

        return communityMap;
    }

    public JSONArray getCommunitys(Token token) throws Exception {
        JSONObject result = doveClient.post("/basic/community/service/list", http->{
            http.addHeader("companyNo", token.getCompanyNo());
        });

        JSONArray data = result.getJSONArray("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }

    public JSONArray getBuildByIds(Token token, List<String> ids) throws Exception {
        JSONObject result = doveClient.post("/basic/building/service/list", http->{
            JSONObject body = new JSONObject();
            body.put("id", ids);

            http.addHeader("companyNo", token.getCompanyNo());
            http.setBody(body.toString());
        });

        JSONArray data = result.getJSONArray("data");

        if (Objects.isNull(data)) {
            return null;
        }

        return data;
    }

    public String getHouseInfo(JSONObject json) {
        StringBuilder build = new StringBuilder(json.getString("buildName"));
        build.append("-");
        String unitName = json.getString("unitName");
        if(Objects.nonEmpty(unitName)){
            build.append(unitName);
            build.append("-");
        }
        build.append(json.getString("floorName"));
        build.append(json.getString("name"));
        return build.toString();
    }

    /**
     * 获取筛选的组织架构ids
     * 返回null 不筛选
     * 返回空集 无命中结果
     */
    public List<String> getDepartmentIdsFilterList(Token token, String departmentId) throws Exception {
        if (StringUtil.isBlank(departmentId)) {
            return Collections.emptyList();
        }

        JSONObject json = doveClient.post("/basic/department/service/list", http->{
            http.addHeader("companyNo", token.getCompanyNo());
            http.addParam("parent_ids", departmentId);
        });

        JSONArray data = json.getJSONArray("data");

        if (Objects.isNull(json) || Objects.isNull(data)) {
            return Collections.emptyList();
        }

        Set<String> departmentIds = new HashSet<>();
        data.forEach(item->{
            JSONObject d = (JSONObject) item;
            departmentIds.add(d.getString("id"));
        });

        // 包含本身
        departmentIds.add(departmentId);

        return new ArrayList<>(departmentIds);
    }
}
